import Vue from 'vue';
import Vuex from 'vuex';
import axios from "axios";
import da from "element-ui/src/locale/lang/da";

Vue.use(Vuex)

const store = new Vuex.Store({
    state: {
        globalVariable:{
            token:{commonRegisterToken:0, adminRegisterToken: 0,commonLoginToken:0},
            account:{studentRegisterAccount:2116191001,teacherRegisterAccount:20222004,adminRegisterAccount:2022103,registerAccount:0},
            rankInfo:{
                student:[],
                teacher:[],
                admin:[]
            }
        },
        accountShowOrNot: false,
        currentUser:{roleId:"1"} ,
        time: "",
        studentAmount: "",
        teacherAmount: "",
        adminAmount: "",
        adminPassword: "",
        loginData: "",

        price: "",
        checkId:"",
        // username:"",
        // useraccount:"",
        
        departmenttime:"",//预约的星期几
        departmenttime2:"",//预约的详细时间 yy年mmm月dd
        departmenttime3:"",//预约的详细时间（没有格式化）
        price1: "",
        price2: "",
        monday:"",
        tuesday:"",
        wednesday:"",
        thursday:"",
        friday:"",
        saturday:"",
        sunday:""

    },
    /*对数据进行过滤*/
    getters: {

    },
    mutations: {
        checkUserLogin(state, data) {
            const that = data.data;
            if (that.account === "" || that.password === "") {
                state.globalVariable.token.commonLoginToken = 1;
                return;
            }
            const length = (state.users.filter(users => users.account == that.account).filter(users => users.password == that.password)).length;

            state.globalVariable.token.commonLoginToken = (length == 0) ? 2 : 3;
        },
        checkRegister(state, register) {
            const that = register.register;
            axios.post("http://localhost:8848/user/registerUser", {
                username: that.username,
                password: that.password,
                roleFlag: that.roleFlag,
            }).then(function (response) {
                    console.log(3123123)
                    console.log(response.data.data);
                }
            );

            if (that.roleFlag === "3") {
                state.globalVariable.token.commonRegisterToken = 1;
                /*设置插入属性的账号*/
            } else if (that.roleFlag === "2") {
                state.globalVariable.token.commonRegisterToken = 1;
            } else if (that.roleFlag === "1" && that.adminCreateAccountToken == true) {
                state.globalVariable.token.adminRegisterToken = 1;
            }
        },
        setStu1(state,val){
            console.log(val.grade);
            console.log(val.uber);
            state.course[val.uber].grade=val.grade;
        },
        addStu1(state,adds){
            console.log(adds);
            var list=state.course;
            list.push(adds);
            console.log(list);
            Vue.set(state,"course",list)
        },
        delStu1(state,index){
            console.log(index);
            var coustu=state.course;
            // index：需要删除的数据的起始位置，n：删除数据的个数
            coustu.splice(index,1);
            console.log(coustu);
        },
        findUserByAccount(state, user) {
            var that = user.user
            var user1 = state.users.filter(users => users.account == that.account)
            state.currentUser = user1[0];
        },
        countUserByRoleId(state, roleId) {
            var that = roleId.roleId;
            if (that.username == "" || that.password == "") {
                state.registerToken = 1;
                return;
            }
            if (that.requirePassword == "") {
                state.registerToken = 2;
                return;
            }
            if (that.requirePassword !== that.password) {
                state.registerToken = 3;
                return;
            }
            if (that.roleId == 1) {
                var countAdmin = (state.users.filter(users => users.roleId == 1)).length;
                state.adminAmount = countAdmin;
            }
            if (that.roleId == 2) {
                var countTeacher = (state.users.filter(users => users.roleId == 2)).length;
                state.teacherAmount = countTeacher;
            }
            if (that.roleId == 3) {
                var countStudent = (state.users.filter(users => users.roleId == 3)).length;
                state.studentAmount = countStudent;
            }
        },
        savemonday(state,value){
            // console.log("savemonday1"+value[0]);//状态
            // console.log("savemonday2"+value[1]);//账号
            state.monday=value[0];
            axios.post("http://localhost:8848/rosteringManage/savemonday", {
              account:value[1],
              monday:value[0]
            }).then(function (response) {
                    console.log(response);
                }
            );
        },
        savetuesday(state,value){
            state.tuesday=value[0];
            axios.post("http://localhost:8848/rosteringManage/savetuesday", {
              account:value[1],
              tuesday:value[0]
            }).then(function (response) {
                    console.log(response);
                }
            );
        },
        savewednesday(state,value){
            state.wednesday=value[0];
            axios.post("http://localhost:8848/rosteringManage/savewednesday", {
              account:value[1],
              wednesday:value[0]
            }).then(function (response) {
                    console.log(response);
                }
            );
        },
        savethursday(state,value){
            state.thursday=value[0];
            axios.post("http://localhost:8848/rosteringManage/savethursday", {
              account:value[1],
              thursday:value[0]
            }).then(function (response) {
                    console.log(response);
                }
            );
        },
        savefriday(state,value){
            state.friday=value[0];
            axios.post("http://localhost:8848/rosteringManage/savefriday", {
              account:value[1],
              friday:value[0]
            }).then(function (response) {
                    console.log(response);
                }
            );
        },
        savesaturday(state,value){
            state.saturday=value[0];
            axios.post("http://localhost:8848/rosteringManage/savesaturday", {
              account:value[1],
              saturday:value[0]
            }).then(function (response) {
                    console.log(response);
                }
            );
        },
        savesunday(state,value){
            state.sunday=value[0];
            axios.post("http://localhost:8848/rosteringManage/savesunday", {
              account:value[1],
              sunday:value[0]
            }).then(function (response) {
                    console.log(response);
                }
            );
        }
    },
    actions: {
        /*当mutation中的方法vue实例被vue实例调用之后，再次提交关闭当前方法的进程*/
        asyncCheckUserLogin(context, payload) {
            /*定时器*/
            // setTimeout(()=>{
            //     context.commit("checkLogin",payload)
            // },2000)
            context.commit("checkUserLogin", payload)
        },
        asyncCheckRegister(context, payload) {
            /*定时器*/
            // setTimeout(()=>{
            //     context.commit("checkLogin",payload)
            // },2000)
            context.commit("checkRegister", payload)
        },
        asyncFindUserByAccount(context, payload) {
            context.commit("findUserByAccount", payload)
        },
        asyncCountUserByRoleId(context, payload) {
            context.commit("countUserByRoleId", payload)
        },
        asyncChange(context, payload) {
            context.commit("change", payload);
        }
    },

})

export default store

